package com.itwn.portal.service;

import com.itwn.portal.entity.Collection;
import com.itwn.portal.mapper.CollectionMapper;
import com.itwn.portal.mapper.ResourceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CollectionService {
    @Autowired
    CollectionMapper collectionMapper;
    @Autowired
    ResourceMapper resourceMapper;

    @Transactional
    public void addCollection(Integer uid, Integer rid) {
        Collection collection = new Collection();
        collection.setUserId(uid);
        collection.setResourceId(rid);

        Collection c=collectionMapper.selectOne(uid,rid);
        if (c==null){
            collectionMapper.insert(collection);
            resourceMapper.updateCollectionNumAdd(rid,1);
        }
    }

    public void removeCollection(Integer uid, Integer rid) {
        Collection collection = new Collection();
        collection.setUserId(uid);
        collection.setResourceId(rid);

        Collection c=collectionMapper.selectOne(uid,rid);
        if (c!=null){
            collectionMapper.delete(collection);
            resourceMapper.updateCollectionNumAdd(rid,-1);
        }
    }

    public int isCol(Integer rid, Integer uid) {
        return this.collectionMapper.isCol(rid,uid);
    }
}
